<?php

// +---------------------------------------------+
// |     Copyright  2010 - 2018 InterPhoto       |
// |     http://www.weentech.com                 |
// |     This file may not be redistributed.     |
// +---------------------------------------------+


include('includes/InterPhoto.Core.php');

header("Expires: Mon, 18 Jul 1988 01:08:08 GMT"); // Date in the past
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // always modified
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0"); // HTTP/1.1
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache"); // HTTP/1.0


$action = ForceIncomingString('action', 'defautform');
if (IsGet('sid') AND IsGet('key')){
	$action = 'getbackpassverify';
}

$smarty = new InterPhoto;
$showform = 0;
$pagenav = '<a href="'.GetUrl('index.php').'">'.$langs['home'].'</a> '.$langs['nav'].' <a href="'.GetUrl('getbackpass.php').'">'.$langs['getbackpass'].'</a>';


if($action == 'getbackpass')
{
	$email           = ForceIncomingString('email');
	$vvckey           = ForceIncomingInt('vvckey');
	$vvc           = ForceIncomingString('vvc');
	if(strlen($email) == 0){
		$errors[] = $sys_langs['please_enter'].$langs['email'];
	}elseif(!IsEmail($email)){
		$errors[] = $sys_langs['bademail'];
	}

	if(!CheckVVC($vvckey, $vvc)){
		$errors[] = $sys_langs['badvvc'];
	}


	if(!isset($errors))
	{
		$user = $DB->query_first("SELECT u.userid, u.username, u.password FROM " . TABLE_PREFIX . "users u LEFT JOIN  " . TABLE_PREFIX . "usergroups ug ON (u.groupid = ug.groupid) WHERE u.email = '$email'  AND u.activated = 1 AND ug.allowlogin = 1");

		if(isset($user)){
			$verifycode = PassGen(8);

			require_once (BASEPATH. 'library/phpmailer/class.phpmailer.php');

			$mail = new PHPMailer();

			if($mainsettings['siteUseSmtp'] != '1'){
				$mail->IsMail();
				$mail->Sender      =  $mainsettings['siteEmail'];
			}else{
				$mail->IsSMTP();
				$mail->Host =  $mainsettings['siteSmtpHost'];
				$mail->Port = $mainsettings['siteSmtpPort'];

				$mail->SMTPAuth = true;
				$mail->Username = $mainsettings['siteSmtpUser'];
				$mail->Password = $mainsettings['siteSmtpPassword'];
				$mail->Sender      =  $mainsettings['siteSmtpEmail'];
			}


			$mail->From      =  $mainsettings['siteEmail'];
			$mail->FromName      =  $mainsettings['siteCopyright'];
			$mail->AddReplyTo ($mainsettings['siteEmail'], $mainsettings['siteCopyright']);   
			
			$mail->Subject  = $langs['getbackpass'];

			$message        = "\r\n" . $user['username']. " ". $langs['getbackpass'].":\r\n\r\n";
			$message        .= $sys_langs['clickforgetback']."\r\n";

			$message       .= "<a href=\"" . BASEURL . "getbackpass.php?key=".base64_encode($user['userid'])."&sid=" . md5($user['username'].COOKIE_KEY.$user['password'].$verifycode) . "\" target=\"_blank\">" .BASEURL . "getbackpass.php?key=".base64_encode($user['userid'])."&sid=" . md5($user['username'].COOKIE_KEY.$user['password'].$verifycode)  . "</a>\r\n";

			$message        .= "\r\n\r\n\r\n\r\n".$mainsettings['siteCopyright']."\r\n".DisplayDate(time())."\r\n";

			$mail->Body     = nl2br($message);
			$mail->AddAddress($email);

			if($mail->Send()){
				$DB->query("UPDATE " . TABLE_PREFIX . "users SET verifycode = '$verifycode' WHERE userid = '$user[userid]'");
				$successtitle = $langs['getbackpass'];
				$success = $sys_langs['getbackneedverify'];
			}else{
				$errors = $sys_langs['sendemailfailed'];
			}

		}else{
			$errors = $sys_langs['noemail'];
		}
	}
	
	if(isset($errors)){
		$errortitle = $langs['getbackpass'].$sys_langs['error'];
		$action = 'defautform';
	}

}


if($action == 'getbackpassverify')
{
	$userid = ForceInt(base64_decode(ForceIncomingString('key')));
	$sid = ForceIncomingString('sid');
	if($userid AND isName($sid)){
		$user = $DB->query_first("SELECT username, password, verifycode, email FROM " . TABLE_PREFIX . "users WHERE activated = 1 AND userid = '$userid'");

		if(isset($user))
		{
			if($sid == md5($user['username'] .COOKIE_KEY.$user['password'].$user['verifycode']))
			{

				$pwd = PassGen(8);

				$DB->query("UPDATE " . TABLE_PREFIX . "users SET password = '".md5($pwd)."', verifycode = '' WHERE userid = '$userid' ");

				require_once (BASEPATH. 'library/phpmailer/class.phpmailer.php');

				$mail = new PHPMailer();

				if($mainsettings['siteUseSmtp'] != '1'){
					$mail->IsMail();
					$mail->Sender      =  $mainsettings['siteEmail'];
				}else{
					$mail->IsSMTP();
					$mail->Host =  $mainsettings['siteSmtpHost'];
					$mail->Port = $mainsettings['siteSmtpPort'];

					$mail->SMTPAuth = true;
					$mail->Username = $mainsettings['siteSmtpUser'];
					$mail->Password = $mainsettings['siteSmtpPassword'];
					$mail->Sender      =  $mainsettings['siteSmtpEmail'];
				}


				$mail->From      =  $mainsettings['siteEmail'];
				$mail->FromName      =  $mainsettings['siteCopyright'];
				$mail->AddReplyTo ($mainsettings['siteEmail'], $mainsettings['siteCopyright']);   
				
				$mail->Subject  = $sys_langs['newpassword'];

				$message        = "\r\n" . $user['username']. " ". $sys_langs['newpassword'].":\r\n\r\n";
				$message        .= $sys_langs['yourusername']." ".$user['username']."\r\n";
				$message        .= $sys_langs['yourpassword']." ".$pwd."\r\n";

				$message        .= "\r\n\r\n\r\n\r\n".$mainsettings['siteCopyright']."\r\n".DisplayDate(time())."\r\n";

				$mail->Body     = nl2br($message);
				$mail->AddAddress($user['email']);

				if($mail->Send()){
					$successtitle = $langs['getbackpass'];
					$success = $sys_langs['newpasswordnote'];
				}else{
					$successtitle = $sys_langs['newpassword'];
					$success = $sys_langs['yourusername'].' '.$user['username'].'<BR>'.$sys_langs['yourpassword'].' '.$pwd;
				}

			}else{
				$verifyfailed = true;
			}
		}else{
			$verifyfailed = true;
		}

	}else{
		$verifyfailed = true;
	}

	if($verifyfailed){
		$errortitle = $sys_langs['emailverify'];
		$errors = $sys_langs['emailverify_err'];
		$action = 'defautform';
	}
}



if($action == 'defautform')
{
	if($userinfo['userid']){
		header("Location: ".GetUrl('mydesk.php'));
		exit();
	}else{
		$showform = 1;
		$smarty->assign('email', $email);
		$smarty->assign('vvckey', CreateVVC());
	}
}

if(!$mainsettings['siteAllowGuest']){
	$smarty->assign('loginforview', $sys_langs['loginforview']);
}else{
	$getimages = $DB->query("SELECT i.imageid, i.path, i.filename, i.title FROM " . TABLE_PREFIX . "images i LEFT JOIN  " . TABLE_PREFIX . "categories c ON (c.categoryid = i.categoryid) WHERE (i.usergroupids = 'all' OR i.usergroupids LIKE '%(".$userinfo['groupid'].")%') AND i.actived = 1 AND c.actived = 1 ORDER BY rand() LIMIT 10");

	$images = $DB->getrows($getimages);

	$smarty->assign('images', $images);
}

$smarty->assign('showform', $showform);
$smarty->assign('pagenav', $pagenav);
$smarty->assign('userinfo', $userinfo);
$smarty->assign('pagetitle', $langs['getbackpass'] . ' - ' .$mainsettings['siteTitle']);

$smarty->interPlay('getbackpass.tpl');

?>